package yt.MajorClass.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import yt.MajorClass.entity.Xgk;
import yt.MajorClass.mapper.XgkMapper;
import yt.MajorClass.service.IXgkService;
import yt.xgk.service.IXgk2025Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/xgk")
public class XgkController {
    @Autowired
    XgkMapper xgkMapper;
    @Autowired
    IXgkService xgkService;

    //查询所有数据
    @ResponseBody
    @GetMapping("/selectAll")
    public List<Xgk> selectAll() {
        return xgkMapper.selectList(null);
    }


    @GetMapping("/selectById")
    @ResponseBody
    public Xgk selectById(@RequestParam(name = "id") Integer id) {
        return xgkMapper.selectById(id);
    }

    @GetMapping("/selectByName")
    @ResponseBody
    public List<Xgk> selectById(@RequestParam(name = "name") String name) {
        QueryWrapper<Xgk> xgkQueryWrapper = new QueryWrapper<>();
        xgkQueryWrapper.like("zymc", name);
        List<Xgk> xgkList = xgkMapper.selectList(xgkQueryWrapper);
        return xgkList;
    }
    //

    @PostMapping("/save")
    @ResponseBody
    public Map<String, Object> save(@RequestBody Xgk xgk) {
        Map<String, Object> map = new HashMap<>();
        //这里书写保存或者是更新我们的数据
        try {
            if (StringUtils.isEmpty(xgk.getId())) {
                xgkMapper.insert(xgk);
            } else {
                xgkMapper.updateById(xgk);
            }
            map.put("sucess", "成功插入");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "用户保存失败");
        }
        return map;
    }


    @GetMapping("selectByPage")
    @ResponseBody
    public List<Xgk> selectByPage(
            @RequestParam(name = "page") Integer page,
            @RequestParam(name = "limit") Integer limit
    ) {
        Page<Xgk> xgkPage = new Page<Xgk>(page, limit);
        Page<Xgk> xgkPage1 = xgkMapper.selectPage(xgkPage, null);
        return xgkPage1.getRecords();

    }




    //查询效果
    //
    @GetMapping("selectByAny")
    @ResponseBody
    public List<Xgk> selectByAny(
            @RequestParam(name = "zymc") String zymc,
            @RequestParam(name = "xlcc") String xlcc,
            @RequestParam(name = "zydm") String zydm,
            @RequestParam(name = "ml") String ml,
            @RequestParam(name = "xk") String xk,
            @RequestParam(name = "zyjs") String zyjs,
            @RequestParam(name = "current") Integer current,
            @RequestParam(name = "limit") Integer limit) {
        return xgkService.selectByAny(zymc, xlcc, zydm, ml, xk, zyjs, current, limit);
    }

}

